<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- public quote -->
<%@include file="/jsp/comm_css_include.jsp"%>
<!-- jqgrid-->
<link href="${basePath}/css/plugins/jqgriddemo/css/ui.jqgrid-bootstrap.min.css" rel="stylesheet" />
<!-- zTreeStyle.css -->
<link rel="stylesheet" type="text/css" href="${basePath}/css/zTreeStyle/zTreeStyle/zTreeStyle.css">
<!doctype html>
<html>
<head>
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="iboxtitle">
			<span><i class="glyphicon glyphicon-map-marker"></i>&nbsp;额外信息推送配置</span>
		</div>
		<div class="ibox-content">
			<button type="button" class="btn dpbtn_green" onclick="add();">新增</button>
			<button type="button" class="btn dpbtn_red" onclick="del();">删除</button>
		</div>
		<div class="ibox-content">
			<div role="form" class="form-inline">
				<div class="form-group">
					<label class="sr-only">消息事件名称：</label> <input type="text"
						placeholder="消息事件名称" id="typeMsgEventName" name="typeMsgEventName" class="form-control">
				</div>
				<div class="form-group">
					<label class="sr-only">业务单位：</label> 
					<input type="text"placeholder="业务单位" id="idUnitName" name="idUnitName"class="form-control">
				</div>
				<input type="hidden" id="salesmanId" />
				<button class="zhixcx" type="button" onclick="query();">执行查询</button>
			</div>
		</div>
		<div class="ibox-content">
			<span class="warning-label"><span class="red-dp-circle"></span>目前只支持征信查询、初审二审、风控总监审核增加额外信息推送</span>
			<div class="jqGrid_wrapper">
				<table id="table_list_1"></table>
				<div id="pager_list_1"></div>
			</div>
		</div>
	</div>
	<div id="hiddenstyle" style="display: none;">
		<div class="padding-nono">
		    <table cellpadding="0" cellspacing="0" border="0" class="buyinfor-table">
				 <tr>
					<td align="right"><label class="control-label np">消息类型：</label>
					</td>
					<td><select id="typeMsgEvent" name="typeMsgEvent" class="select-control"></select></td>
				</tr>
				<tr>
					<td align="right" valign="top"><label class="control-label np">消息状态：</label>
					</td>
					<td class="statusMsgHandleTd" colspan="3"> </td>
				</tr>
				<tr>
					<td align="right">
						<label class="control-label np">业务单位：</label>
					</td>
					<td> 
						<input id="replaceId" type="hidden">
						<input id="codeUnit" type="hidden" />
						<input id="idUnit" type="hidden"/>
						<input class="form-control smalliconcur smallicon" readonly="readonly" type="text" onclick="branchOrg();" id="orgName" name="orgName" placeholder="选择业务单位" />
					</td>	
				</tr>
				<tr>
					<td align="right">
						<label class="control-label np">消息处理状态：</label>
					</td>
					<td> 
						<select class="form-control" id="valueMsgHandle" name="valueMsgHandle">
							<option value="0">不处理</option>
							<option value="1">需要处理</option>
						</select>
					</td>	
				</tr>
				<tr>	
					<td align="right">
						<label class="control-label np">选择账号：</label>
					</td>
					<td>
						<input id="ids" type="hidden">
						<input class="form-control smalliconcur smallicon" readonly="readonly" onclick="userLogin();" id="user" name="user" placeholder="选择账号" class="" />
					</td>
				</tr>
				<tr>
					<td align="right" valign="top">
						<label class="control-label np">备注：</label>
					</td>
					<td colspan="3"> 
						<textarea rows="2" id="remarks" cols="" class="form-control"></textarea>
					</td>	
				</tr>
			</table>
			<div class="padding-nono" >
				<table cellpadding="0" cellspacing="0" border="0" bordercolor="#3B95C8"  width="100%">
					<tr>
						<td width="100%" align="center">
							 <div class="ibox-content">
							 	  <button type="button" class="btn dpbtn_red" onclick="saveData();">保存</button>
					              <button type="button" class="btn dpbtn_grey" onclick="cancel();">取消</button>
					          </div>
						</td>
					</tr>
				</table>
			</div>	
		</div>
	</div>	
	
	<!-- //账号 -->
	<div style="display: none;" id="userLoginDiv">
		<div class="ibox-content">
			<div role="form" class="form-inline">
				<div class="form-group">
					<input type="text"placeholder="姓名" id="name" name="name" class="form-control">
				</div>
				<div class="form-group">
					<input type="text" placeholder="手机号码" id="phone" name="phone" class="form-control">
				</div>
				<input type="hidden" id="salesmanId" />
				<button type="button" class="btn dpbtn_green" onclick="ZxQuery();">查询</button>
				<button type="button" class="btn dpbtn_red" onclick="saveButton();">确认</button>
				<button type="button" class="btn dpbtn_grey" onclick="buttonClose();">关闭本页</button>
			</div>
		</div>
		<div class="ibox-content">
			<div class="jqGrid_wrapper">
				<table id="table_list_2"></table>
				<div id="pager_list_2"></div>
			</div>
		</div>
	</div>	
	<!-- public quote -->
	<%@include file="/jsp/comm_js_include.jsp"%>
	<!-- jqGrid -->
	<script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" charset="utf-8" src="${basePath}/js/plugins/jqgriddemo/js/grid.locale-cn.min.js"></script>
	<!--zTreeStyle.js-->
	<script type="text/javascript" src="${basePath}/js/zTreeStyle/jquery.ztree.core.js" charset="utf-8"></script>
	<script type="text/javascript" src="${basePath}/js/zTreeStyle/jquery.ztree.excheck.js" charset="utf-8"></script>
	<!-- custom js -->
	<%@include file="/js/common-dpqc/dpqc-validate/comm_validate_include.jsp"%>
	<!--
    	作者：zhangpp
    	时间：2017-04-06
    	描述：js
    -->
	<script type="text/javascript">
	    
	    var winWidth = $(window).width();
		var winHeight = $(window).height();
	    var indexDw;
	    
	    $(function(){
			$.dpAjax({
		    	url:'${basePath}/DataDictionaryController/getEnableList',
	 			data:{},
	 			success: function(datas){
 					  var result = datas.result.rows;
 					  var programme ='<option value="">请选择</option>';
 					  var html='';
 				      for(var i=0;i<result.length;i++){
 				    	 if(result[i].parentValue == "MessageEventType"){
 				    		programme +='<option value="'+result[i].value+'">'+result[i].name+'</option>'
				    	 }
 				    	 if(result[i].parentValue == "MessageHandleStatus"){
 				    		html += '<label style="display:block;margin-right:20px;float:left; margin-top:8px;"><input type="checkbox" name="statusMsgHandle" value="'+result[i].value+'" />'+result[i].name+'</label>';
				    	 }
 				      }
 				     $("#typeMsgEvent").html(programme);
 				     $(".statusMsgHandleTd").html(html);
	            }
		    });
			getData();
			userLoading();
		 });
	    
	    var getData = function()
	    {
	       var winWidth = $('div.ibox-content').width();
	       var winHeight = document.body.offsetHeight - 400;
	       var win = winWidth / 5 - 20;
  	 	   $.jgrid.defaults.styleUI = 'Bootstrap';
  	 	   
		  jQuery("#table_list_1").jqGrid
		  ({
			url:'${basePath}/OtherMessagePushExtraController/list',
	        datatype : "json",
	        colNames : [ '主键', '','消息事件类型','','消息处理状态','','消息处理值', 'userId','账号','','','业务单位','remarks'],
	        colModel : [ 
	                     {name: 'replaceId', hidden:true},
	            		 {name: 'typeMsgEvent',hidden:true},
	            		 {name: 'typeMsgEventName',width: win,align: "center",sortable:false},
	            		 {name: 'statusMsgHandle',hidden:true },
	            		 {name: 'statusMsgHandleName',width: win,align: "center",sortable:false,
	            			 formatter:function(a,b,c)
	            			 {
	            				 if(c.statusMsgHandle == "01")
	            				 {
	            					 return "发短信";
	            				 }
	            				 if(c.statusMsgHandle == "02")
	            				 {
	            					 return "发送APP推送";
	            				 }
	            				 if(c.statusMsgHandle == "03")
	            				 {
	            					 return "发送站内信";
	            				 }
	            				 if(c.statusMsgHandle == "01,02")
	            				 {
	            					 return "发送APP推送";
	            				 }
	            				 if(c.statusMsgHandle == "01,03")
	            				 {
	            					 return "发短信,发送站内信";
	            				 }
	            				 if(c.statusMsgHandle == "02,03")
	            				 {
	            					 return "发送APP推送,发送站内信";
	            				 }
	            				 if(c.statusMsgHandle == "01,02,03")
	            				 {
	            					 return "发短信,发送APP推送,发送站内信";
	            				 }
	            			 }	 
	            		 },
 	            		 {name: 'valueMsgHandle',hidden:true},
	            		 {name: 'valueMsgHandleName',width: win,align: "center",
 	            			formatter:function(a,b,c)
 	            			{
 	            				if(c.valueMsgHandle == "0")
		                       	{
		                       		return "不处理";
		                       	}
		                       	return  "需要处理";
 	            			}	 
	            		 },
	            		 {name: 'userId',hidden:true},
	               		 {name: 'userName',width: win,align: "center",sortable:false},
	               		 {name: 'idUnit',hidden:true},
	               		 {name: 'codeUnit',hidden:true},
	               		 {name: 'idUnitName',width: win,align: "center",sortable:false},
	               		 {name: 'remarks',hidden:true}
	                   ], 
              mtype : "post",
              height: winHeight,
              width:winWidth,  
              autowidth: false,
              shrinkToFit: false,
              rowNum: 15,
	          rowList: [15, 30, 45],
              viewrecords: true,
              pager: "#pager_list_1",
              hidegrid: false,
              multiselect : true,
              altRows:true,
              altclass:'someClass',
              ondblClickRow:function(rowId,v,e)
              {
            	  var rowData = $("#table_list_1").jqGrid("getRowData", rowId);
            	  var replaceId = rowData.replaceId;
            	  var typeMsgEvent = rowData.typeMsgEvent;
            	  var statusMsgHandle = rowData.statusMsgHandle;
            	  $('.statusMsgHandleTd input[name="statusMsgHandle"]').each(function(index){
	   				   if(statusMsgHandle.indexOf(this.value) >= 0){
	   					   this.checked = true;
	   				   }
	   			  });
            	  $("#typeMsgEvent").val(rowData.typeMsgEvent);
            	  $("#valueMsgHandle").val(rowData.valueMsgHandle);
            	  $("#remarks").val(rowData.remarks);
            	  
            	  $('.statusMsgHandleTd input[name="statusMsgHandle"]').each(function(index){
	   				   if(statusMsgHandle.indexOf(this.value) >= 0){
	   					   this.checked = true;
	   				   }
	   			  });
            	  
            	  var userId = rowData.userId;
            	  
		  		  $("#ids").val(userId);
		  		  $("#replaceId").val(replaceId);
            	  $("#userLogin li").each(function(index){
            		  var idItems = $(this).find("span.backImg").attr("attrId");
            		  if(userId.indexOf(idItems) >= 0){
            			  $(this).find("span.backImg").addClass("proactiveIcon");
            		  }
            	  })
            	  $("#user").val(rowData.userName);
            	  $("#idUnit").val(rowData.idUnit);
            	  $("#codeUnit").val(rowData.codeUnit);
            	  $("#orgName").val(rowData.idUnitName);
            	  
          		  layer.open({
	      	    	  title:'额外信息推送配置',
	      	    	  type: 1,
	      	    	  area: ['420px', '500px'], //宽高
	      	    	  content: $("#hiddenstyle"),
	      	    	  end:function(){
			    		  $("#userLogin li").each(function(index){
			    			  $(this).find("span.backImg").removeClass("proactiveIcon");
		            	  })
	  				  }
      	    		});
              },
              jsonReader : {
            	  root:"result.rows",
            	  page: "result.page",
            	  total: "result.total",
            	  records: "result.records",
            	  repeatitems: false,
            	  id: "replaceId"
              },
              loadComplete:function(datas){
            	  if (datas.flag == "100100") 
            	  {
            		  layer.msg(datas.message, {icon: 1});
                      return;
                  }
            	  if(datas.flag == '100802')
            	  {
    				  top.location.href = thisLoginUrl;
    				  return;
    			  }
            	  layer.msg(datas.message, {icon: 2});
              }
	      });
	    }
	    
	    window.onresize = function()
	    {
	    	$("#table_list_1").setGridWidth($(window).width()-80);
	    }
	    
	    var query = function()
	    {
	    	var postData = $("#table_list_1").jqGrid("getGridParam", "postData");
	    	var data = {};
	    	data.typeMsgEventName = document.getElementById('typeMsgEventName').value || '';
	    	data.idUnitName = document.getElementById('idUnitName').value || '';
	    	$.extend(postData, data);
	    	refresh();
	    }
	    
	    var refresh = function()
	    {
	    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", [{ page: 1}]);
	    }
	    
	    
	    var branchOrg = function(){
 			$.dpAjax({
 	    		url:'${basePath}/BaseBusiUnitController/list',
 	    		data:{},
     			success: function(datas)
     			{
    				  var content = '<div class="ibox-content" style="height: 480px;border: 1px solid #F5F5F6; overflow:auto;overflow-x: hidden;">'
	    					+ '	<div id="orgtreeDemo" class="ztree">&nbsp;</div>'
	    				+ '</div>'
	    				+ '<div class="ibox-content">'
	    				+ '	 <button class="btn dpbtn_green" type="button" onclick="confirmOrg();">确定</button>'
	    				+ '	 <button class="btn dpbtn_red" type="button" onclick="cancelOrg();">取消</button>'
	    				+ '</div>';
	    		
	    				indexDw = layer.open({
					    	  title:'分配单位',
					    	  type: 1,
					    	  area: ['800px', '600px'], //宽高
					    	  content: content
					    });
					    var setting = {
						check: {
							enable: true
						},
						data: {
							simpleData: {
								enable: true
							},
						}
					    };			
					  var  arr = [];
    				  var result = datas.result.rows;
    				  for(var i in result)
    				  {  
    					  var objs = {};
    					  objs.replaceId = result[i].replaceId;
    					  objs.id = result[i].orgCode;
    					  objs.pId = result[i].parentCode;
    					  objs.name = result[i].orgName;
    					  if(result[i].parentCode == '0')
    					  {
            		  	 	objs.open = true;
    					  } 
    					  else 
    					  {
    						 objs.open = false; 
    					  }
    					  objs.checked = result[i].checked;
	            		  objs.salersman=result[i].salersman;
    					  arr.push(objs);
    				  }
    				  zTrees_ = $.fn.zTree.init($("#orgtreeDemo"), setting, arr);
    				  zTrees_.setting.check.chkboxType={"Y": "", "N": "ps"};
					  return;
                 }
 	    	});
 	    }
    	 
	    var confirmOrg = function()
	    {
			var treeObj = $.fn.zTree.getZTreeObj("orgtreeDemo");
			var nodes = treeObj.getCheckedNodes(true);
	        var length = nodes.length;
	        if(length == 0) {
	        	layer.msg('请选择分配的业务单位',{icon:7});
	        	return;
	        }
	        
	        var idUnit=''; 
			var codeUnit='';
			var orgName='';
			for (var i = 0; i < nodes.length; i++) 
			{  
               	 idUnit  += nodes[i].replaceId +',';
               	 codeUnit +=nodes[i].id+',';
               	 orgName += nodes[i].name+',';
            }
			idUnit = idUnit.substring(0,idUnit.lastIndexOf(","));
			codeUnit = codeUnit.substring(0,codeUnit.lastIndexOf(","));
			orgName = orgName.substring(0,orgName.lastIndexOf(","));
			$("#orgName").val(orgName);
			$("#idUnit").val(idUnit);
			$("#codeUnit").val(codeUnit);
			layer.close(indexDw);
	    }
		
		var cancelOrg = function()
	    {
			layer.closeAll();	
	    }
		
		var cancel = function()
	    {
			layer.closeAll();	
	    }
	    
		var indexZh;
		
		var userLogin = function()
	    {
			indexZh = layer.open({
		    	  title:'选择账户',
		    	  type: 1,
		    	  area: ['800px', '550px'], //宽高
		    	  content: $("#userLoginDiv")
		    });
	    }
		
		var userLoading = function(){
		      var win = 800 / 4;
	  	 	  $.jgrid.defaults.styleUI = 'Bootstrap';
			  jQuery("#table_list_2").jqGrid
			  ({
				url:'${basePath}/user/getList',
		        datatype : "json",
		        colNames : [ '主键', '登录名称', '电话号码','名称'],
		        colModel : [ 
		                     {name: 'replaceId', index: 'id', hidden:true},
		            		 {name: 'loginName',index: 'loginName',width: win,align: "center"},
		            		 {name: 'phone',index: 'phone',width: win,align: "center"},
		            		 {name: 'name',index: 'name',width: win,align: "center"},
		                   ],
	              mtype : "post",
	              height: 300,
	              width:750,  
	              autowidth: false,
	              shrinkToFit: false,
	              rowNum: 15,
		          rowList: [15, 30, 45],
	              viewrecords: true,
	              pager: "#pager_list_2",
	              hidegrid: false,
	              multiselect : true,
	              altRows:true,
	              altclass:'someClass',
	              jsonReader : {
	            	  root:"result.rows",
	            	  page: "result.page",
	            	  total: "result.total",
	            	  records: "result.records",
	            	  repeatitems: false,
	            	  id: "replaceId"
	              },
	              loadComplete:function(datas){
	            	  if (datas.flag == "100100") {
	                      layer.msg("查询成功!", {icon: 6, time: 1000});
	                      return;
	                  }
	            	  if(datas.flag == '100802') {
	    				  top.location.href = thisLoginUrl;
	    				  return;
	    			  }

	            	  layer.msg(datas.message, {icon: 2});
	              }
		      });
	    }
		
		var ZxQuery = function()
	    {
	    	var postData = $("#table_list_2").jqGrid("getGridParam", "postData");
	    	var data = {};
	    	data.phone = document.getElementById('phone').value || '';
	    	data.name = document.getElementById('name').value || '';
	    	$.extend(postData, data);
	    	refreshLS();
	    }
	    
		var buttonClose = function()
	    {
			layer.confirm('关闭本页不会选择任何账户哦', { btn: ['确定','取消'] },function(){
				layer.close(indexZh);
				layer.closeAll('dialog');
			})
			
	    }
		
	    var refreshLS = function()
	    {
	    	$("#table_list_2").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", [{ page: 1}]);
	    }
	    
		function clickThis(obj)
		{
			$(obj).find(".backImg").toggleClass("proactiveIcon");
		}
		
		function ResetButton()
		{
			$("#userLogin").find("li span.backImg").removeClass("proactiveIcon");
		}
		
		function saveButton()
		{
			var ids = $("#table_list_2").jqGrid("getGridParam", "selarrrow");
			var id = '';
			var users = '';
			for(var i=0;i<ids.length;i++)
			{
			   var rowData = $("#table_list_2").jqGrid('getRowData',ids[i]);
			   var idsItem = rowData.replaceId;
			   var user = rowData.name;
			   if(idsItem && idsItem !=""){
					id += idsItem + ',';
			   }
			   if(user && user !=""){
				  users += user + ',';
			   }
			}
			
			id = id.substring(0,id.lastIndexOf(","));
			users = users.substring(0,users.lastIndexOf(","));
			
			if(id == "")
	        {
	        	layer.msg("请选择账户！", {icon: 5});
	        	return;
	        }
			
			$("#ids").val(id);
			$("#user").val(users);
			layer.close(indexZh);
		}
		
		
		var saveData = function()
		{
			var data ={};
			var value ="";
	        $('.statusMsgHandleTd input[name="statusMsgHandle"]:checked').each(function(){
	        	value += $(this).val()+",";
	        });
	        value = value.substring(0,value.lastIndexOf(","));
	        
	        data.statusMsgHandle = value;
	        data.typeMsgEvent = $("#typeMsgEvent").val();
	        data.codeUnit = $("#codeUnit").val();
	        data.idUnit = $("#idUnit").val();
	        data.userId = $("#ids").val();
	        data.remarks = $("#remarks").val();
	        data.userName = $('#user').val();
	        data.valueMsgHandle = $('#valueMsgHandle').val();
	        id = $('#replaceId').val();
	        
	        if(data.typeMsgEvent == "" || !data.typeMsgEvent)
	        {
	        	layer.msg("消息类型不能为空", {icon: 2});
	        	return;
	        }
	        
	        if(data.statusMsgHandle == "" || !data.statusMsgHandle)
	        {
	        	layer.msg("消息状态不能为空", {icon: 2});
	        	return;
	        }
	        
	        if(data.idUnit == "" || !data.idUnit)
	        {
	        	layer.msg("业务单位不能为空", {icon: 2});
	        	return;
	        }
	        
	        if(data.userName == "" || !data.userName)
	        {
	        	layer.msg("选择账户不能为空", {icon: 2});
	        	return;
	        }
	        
	        if(id && id != "")
	        {
	        	url = '${basePath}/SystemController/otherMessagePushExtraMapperUpdate';
	        	data.id = $('#replaceId').val();
	        }
	        else
	        {
	        	url = '${basePath}/SystemController/otherMessagePushExtraMapperInsert';
	        }
	        
	        $.dpAjax({
	        	url:url,
	   			data:data,
	   			success: function(datas)
	   			{
	   				layer.msg(datas.message, {icon: 6});
	   				resetData();
	   				refresh();
				 	layer.closeAll();
					return;
	   			}
	        })
		}
		
		//添加
		var add = function()
		{
			layer.open({
   	    	  title:'额外信息推送配置',
   	    	  type: 1,
   	    	  area: ['420px', '500px'],
   	    	  content: $("#hiddenstyle")
    		});
		}
		
		var del = function(){
			var ids = $("#table_list_1").jqGrid("getGridParam", "selarrrow"); 
			var idsItem = "";
			for(var i=0;i<ids.length;i++){
			  var rowData = $("#table_list_1").jqGrid('getRowData',ids[i]);
			  var replaceId = rowData.replaceId; 
			  idsItem += replaceId + ",";
			}
			
			idsItem = idsItem.substring(0,idsItem.lastIndexOf(","));
			
			$.dpAjax({
	        	url: '${basePath}/SystemController/otherMessagePushExtraMapperDel ',
	   			data:{
	   				"ids":idsItem
	   			},
	   			success: function(datas)
	   			{
	   				layer.msg(datas.message, {icon: 6});
	   				refresh();
					return;
	   			}
	        })
		} 
		
		var refresh = function()
	    {
	    	$("#table_list_1").jqGrid("setGridParam", { search: true }).trigger("reloadGrid", []);
	    }
		
		var resetData = function(){
			$("#typeMsgEvent").val("");
	        $("#codeUnit").val("");
	        $("#idUnit").val("");
	        $("#ids").val("");
	        $("#remarks").val("");
	        $('#user').val("");
	        $('#valueMsgHandle').val("0");
	        $('#replaceId').val("");
		}
    </script>
</body>
</html>
